import { observable, action } from 'mobx';
import { Bubble } from 'weaver-mobile';
import { RouteLayout, ListViewPage } from 'weaver-mobile-page';
import NewGroupVoteStore from './New';
import VoteStore from './Vote';

import * as Api from '../apis';

const initState = {
  voteListLoading: true,
  shareVisible: false,
  shareChoose: false,
  isSearch: false,
}
export default class GroupVoteStore {
  @observable newGroupVoteStore = new NewGroupVoteStore();
  @observable voteStore = new VoteStore();
  @observable newVoteLayout = new RouteLayout.Store({ route: '/groupVote', id: 'new'});
  @observable votingLayout = new RouteLayout.Store({ route: '/groupVote', id: 'voting'});
  @observable votedLayout = new RouteLayout.Store({ route: '/groupVote', id: 'voted'});
  @observable voteList = new ListViewPage.Store({ route: '/groupVote', id: 'list' });
  @observable voteListLoading = initState.voteListLoading;
  @observable shareVisible = initState.shareVisible; // popver 显隐
  @observable shareChoose = initState.shareChoose; // 批量分享
  @observable isSearch = initState.isSearch; 

  @action setStoreState = (params) => {
    Object.keys(params).forEach(key => {
      this[key] = params[key];
    })
  }

  /* 列表请求 */
  @action getGroupVoteList = (params) => {
    params = {
      groupid: '',
      isgroup: '', // 1：是， 0：否
      ...params
    }
    Api.getGroupVoteList(params).then(res => {
      const dataKey = res.sessionkey;
      this.voteList.getData({ dataKey }, () => this.voteListLoading = false, true)
    })
  }

  // 取消批量
  @action doShareCancel = () => {
    this.shareChoose = false;
    this.doShareSelectNone();
  }

  // 全选
  @action doShareSelectAll = () => this.voteList.selectAll();

  // 取消全选
  @action doShareSelectNone = () => this.voteList.selectNone();

  // 批量分享
  @action groupchatvotealert = (params) => {
    Api.groupchatvotealert(params).then(res => {

    })
  }

  // 删除群投票
  @action groupchatvotedel = (params) => {
    Api.groupchatvotedel(params).then(res => {
      if (res.api_status === 'success') {
        this.getGroupVoteList();
        Bubble.success('删除成功！');
      } else {
        Bubble.error('删除失败，请重试！');
      }
    })
  }
}